package com.cc.pokergame.utils;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * 封装动画工具类
 */
public class AnimatorUtils {
    // 淡入
    public static void alphaIn(View target) {
        // 显示
        target.setVisibility(View.VISIBLE);
        // 创建属性动画对象
        // ObjectAnimator.ofFloat(定位到组件, "动画名称", 多个时间)
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(target, "alpha", 0.0f, 1.0f);
        // 设置动画时长
        objectAnimator.setDuration(500);
        // 开启动画
        objectAnimator.start();

        // 设置控件点击状态，false->禁止点击，默认为true
        // gameBinding.gameButton.setEnabled(false);
    }

    // 淡出
    public static void alphaOut(View target) {
        // 创建属性动画对象
        // ObjectAnimator.ofFloat(定位到组件, "动画名称", 多个时间)
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(target, "alpha", 1.0f, 0.0f);
        // 设置动画时长
        objectAnimator.setDuration(500);
        // 开启动画
        objectAnimator.start();
        // 隐藏
        target.setVisibility(View.INVISIBLE);
        // 设置控件点击状态，false->禁止点击，默认为true
        // gameBinding.gameButton.setEnabled(false);
    }

    // x轴移动
    public static void translate(View target, float fromX, float toX) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "x", fromX, toX);
        animator.setDuration(1000);
        animator.setInterpolator(new BounceInterpolator());
        animator.start();
    }

    // 反转，以Y轴
    public static void rotateY(View target, float... degrees) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(target, "rotationY", degrees);
        objectAnimator.setDuration(500);
        objectAnimator.start();
    }

    // 缩放动画
    public static void scaleInOut(View target, float from, float to) {
        // 补间动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(from, to, from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);
        // 设置重复次数 INFINITE表示一直重复
        scaleAnimation.setRepeatCount(Animation.INFINITE);
        // 设置重复模式
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        target.startAnimation(scaleAnimation);
    }

    // 缩放动画
    public static void scaleY(View target, float from, float to) {
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, "scaleY", from, to);
        scaleY.setDuration(500);
        scaleY.start();
    }

    // 缩放动画
    public static void scale(View target, float from, float to) {
        // 补间动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(from, to, from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(300);
        target.startAnimation(scaleAnimation);
    }

    // 缩放动画
    public static void scaleTop(View target, int from, int to) {
        // 属性动画
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) target.getLayoutParams();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(animation -> {
            Integer value = Integer.valueOf(animation.getAnimatedValue().toString());
            layoutParams.topMargin = value;
            target.setLayoutParams(layoutParams);
        });

        valueAnimator.start();
    }
}
